package com.faxsun.helper;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections4.CollectionUtils;

public class HTMLHelper {

	public static Set<String> getImgs(String content) {

		String img = "";

		Pattern p_image;

		Matcher m_image;

		String str = "";

		String[] images = null;

		String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";

		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);

		m_image = p_image.matcher(content);

		while (m_image.find()) {

			img = m_image.group();

			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)")
					.matcher(img);

			while (m.find()) {

				String tempSelected = m.group(1);

				if ("".equals(str)) {

					str = tempSelected;

				} else {

					String temp = tempSelected;

					str = str + "," + temp;

				}

			}

		}

		if (!"".equals(str)) {

			images = str.split(",");

		}
		Set<String> set = new LinkedHashSet<String>();

		if (images != null)
			CollectionUtils.addAll(set, images);
		return set;

	}
}
